﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-16
Description:Sms_Sended三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class Sms_SendedService
	{
		#region 增加一条数据
		public static int AddSms_Sended(Sms_Sended sms_Sended)
		{
			string sql =
			"INSERT INTO Sms_Sended(smsguid,result,channelID,resulttime) "+
			"VALUES (@smsguid,@result,@channelID,@resulttime)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@smsguid",sms_Sended.Smsguid),
						
					new SqlParameter("@result",sms_Sended.Result),
						
					new SqlParameter("@channelID",sms_Sended.ChannelID),
						
					new SqlParameter("@resulttime",sms_Sended.Resulttime)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSms_SendedById(long id)	
		{
			string sql="DELETE "+"Sms_Sended"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySms_SendedByid(Sms_Sended sms_Sended)
		{
		string sql ="UPDATE "+"Sms_Sended"+" set "+
					"smsguid=@smsguid, "+
					"result=@result, "+
					"channelID=@channelID, "+
					"resulttime=@resulttime "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",sms_Sended.Id),
					new SqlParameter("@smsguid",sms_Sended.Smsguid),
					new SqlParameter("@result",sms_Sended.Result),
					new SqlParameter("@channelID",sms_Sended.ChannelID),
					new SqlParameter("@resulttime",sms_Sended.Resulttime)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Sms_Sended> GetAllSms_Sendeds()
		{
			string sqlAll = "SELECT * FROM Sms_Sended";
			return GetSms_SendedsBySql(sqlAll);
		}
		//以标识列获得对象
		public static Sms_Sended GetSms_SendedByid(object id)
		{
			string sql = "SELECT * FROM "+"Sms_Sended"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Sms_Sended sms_Sended = new Sms_Sended();
					sms_Sended.Id=Convert.ToInt32(reader["id"]);
					sms_Sended.Smsguid=Convert.ToString(reader["smsguid"]);
					sms_Sended.Result=Convert.ToString(reader["result"]);
					sms_Sended.ChannelID=Convert.ToString(reader["channelID"]);
					sms_Sended.Resulttime=Convert.ToDateTime(reader["resulttime"]);
					reader.Close();
					
					return sms_Sended;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Sms_Sended> GetSms_SendedsBySql(string safeSql)
		{
			List<Sms_Sended> list = new List<Sms_Sended>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_Sended sms_Sended = new Sms_Sended();
					sms_Sended.Id=Convert.ToInt32(row["id"]);
					sms_Sended.Smsguid=Convert.ToString(row["smsguid"]);
					sms_Sended.Result=Convert.ToString(row["result"]);
					sms_Sended.ChannelID=Convert.ToString(row["channelID"]);
					sms_Sended.Resulttime=Convert.ToDateTime(row["resulttime"]);
					list.Add(sms_Sended);
				}
				return list;
			}
             
		}
		private static IList<Sms_Sended> GetSms_SendedsBySql( string sql, params SqlParameter[] values )
		{
			List<Sms_Sended> list = new List<Sms_Sended>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_Sended sms_Sended = new Sms_Sended();
					sms_Sended.Id=Convert.ToInt32(row["id"]);
					sms_Sended.Smsguid=Convert.ToString(row["smsguid"]);
					sms_Sended.Result=Convert.ToString(row["result"]);
					sms_Sended.ChannelID=Convert.ToString(row["channelID"]);
					sms_Sended.Resulttime=Convert.ToDateTime(row["resulttime"]);
					list.Add(sms_Sended);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<Sms_Sended> ShowSms_SendedForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from Sms_Sended where  id not in ( select top "
						   +number+" id from Sms_Sended order by id desc ) order by id desc";
			return GetSms_SendedsBySql(strsql);
		}
		#endregion
	}
}

